package com.ruoyi.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sales.domain.ZcSpecifications;
import com.ruoyi.sales.service.IZcSpecificationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品规格Controller
 * 
 * @author woo
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/sales/specifications")
public class ZcSpecificationsController extends BaseController
{
    @Autowired
    private IZcSpecificationsService zcSpecificationsService;

    /**
     * 查询产品规格列表
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcSpecifications zcSpecifications)
    {
        startPage();
        List<ZcSpecifications> list = zcSpecificationsService.selectZcSpecificationsList(zcSpecifications);
        return getDataTable(list);
    }

    /**
     * 导出产品规格列表
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:export')")
    @Log(title = "产品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcSpecifications zcSpecifications)
    {
        List<ZcSpecifications> list = zcSpecificationsService.selectZcSpecificationsList(zcSpecifications);
        ExcelUtil<ZcSpecifications> util = new ExcelUtil<ZcSpecifications>(ZcSpecifications.class);
        util.exportExcel(response, list, "产品规格数据");
    }

    /**
     * 获取产品规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:query')")
    @GetMapping(value = "/{specificationsId}")
    public AjaxResult getInfo(@PathVariable("specificationsId") Long specificationsId)
    {
        return success(zcSpecificationsService.selectZcSpecificationsBySpecificationsId(specificationsId));
    }

    /**
     * 新增产品规格
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:add')")
    @Log(title = "产品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcSpecifications zcSpecifications)
    {

        if (!zcSpecificationsService.checkSpecificationsUnique(zcSpecifications))
        {
            return error("新增产品规格失败，该产品规格已存在");
        }
        return toAjax(zcSpecificationsService.insertZcSpecifications(zcSpecifications));
    }

    /**
     * 修改产品规格
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:edit')")
    @Log(title = "产品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcSpecifications zcSpecifications)
    {
        if (!zcSpecificationsService.checkSpecificationsUnique(zcSpecifications))
        {
            return error("修改产品规格失败，该产品规格已存在");
        }
        return toAjax(zcSpecificationsService.updateZcSpecifications(zcSpecifications));
    }

    /**
     * 删除产品规格
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:remove')")
    @Log(title = "产品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specificationsIds}")
    public AjaxResult remove(@PathVariable Long[] specificationsIds)
    {
        return toAjax(zcSpecificationsService.deleteZcSpecificationsBySpecificationsIds(specificationsIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('sales:specifications:edit')")
    @Log(title = "产品规格", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ZcSpecifications zcSpecifications)
    {
        return toAjax(zcSpecificationsService.updateSpecificationsStatus(zcSpecifications));
    }
}
